<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    queryRatePieChartsByGroupsZy:{
      type: Function,
      default: null
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      var that = this;
      this.chart.on('click',function(params){
      var index = params.dataIndex;
      var data = that.chartData.groupsdata[index];
      var params = {};
      params.groups = data;
      params.type = "2";
　　　  that.queryRatePieChartsByGroupsZy(params);//调用接口
      })
    },
    setOptions( {zydata,zyname}  = {}) {
      this.chart.setOption({
        color: ['#3398DB'],
        title: {
        text: zyname,
        textStyle: {
              fontSize: 15,
              color: "#00FFE3"
            },
            left: '6',
            top: "10"
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c}%'
    },
    series: [
        {
            name: '差错率',
            type: 'pie',
            radius: '50%',
            data: zydata,
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }
    ]
     })
    }
  }
}
</script>
